package com.ChenYaRou.www.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName Secret
 * @Description  对密码进行加密
 * @Author 86183
 * @Date 2022/4/20 19:25
 * @Version 1.0
 **/
public class Secret {
    /**
     * 密码加密
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String turnSecret(String password) throws NoSuchAlgorithmException {
        if(password==null){
            return null;
        }
        MessageDigest md5=MessageDigest.getInstance("MD5");
        md5.update(password.getBytes());
        byte[] digest = md5.digest();
        StringBuilder builder=new StringBuilder();
        for (int b : digest) {
            int i = b;
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                builder.append("0");
            }
            builder.append(Integer.toHexString(i));
        }
        return builder.substring(8,24);

    }
}
